<?php
/**
 * Created by PhpStorm.
 * User: zhiduopin-PC
 * Date: 2018/10/26
 * Time: 14:31
 */
namespace app\admin\model;

class Exchange extends DataBase{
    protected $autoWriteTimestamp = true;
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';
    //获取器
    public function getNicknameAttr($value){
        return conversionEmojiName($value);
    }
    /**
     * Notes: 兑换记录数据
     * User: 王俊成
     * Date: 2018/10/30
     * Time: 11:11
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function exchangeData(){
        $page_config = array(
            'page' => input('get.pageNum',1),
            'row_lists' => input('get.limit',1)
        );
        $keyWords = input('get.keyWords');
        $startTime = input('get.start_time');
        $endTime = input('get.end_time');
        $map = [];
        if(!empty($keyWords) || $keyWords == '0'){
            $key =input('key');
            if($key =='nickname')
                $map[] = ['b.'.$key,'like','%'.$keyWords.'%'];
            if($key == 'u_id')
                $map[] = ['b.'.$key,'=',$keyWords];
        }
        if(!empty($startTime) && !empty($endTime)){
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
            if($startTime <= $endTime)
                $map[] = ['a.create_time','between',[$startTime,$endTime]];
            else
                $map[] = ['a.create_time','between',[$endTime,$startTime]];
        }
        else if(!empty($startTime)){
            $startTime = strtotime($startTime);
            $map[] = ['a.create_time','>=',$startTime];
        }
        else if(!empty($endTime)){
            $endTime = strtotime($endTime);
            $map[] = ['a.create_time','<=',$endTime];
        }
        return $this
            ->alias('a')
            ->field('a.id,a.user_id,b.nickname,b.u_id,a.exchange_money,a.exchange_diamonds,a.create_time')
            ->leftJoin('users b','a.user_id = b.id')
            ->where($map)->order('a.create_time','desc')->paginate($page_config);
    }

    /**
     * @return array
     * @user 帅帅吴
     * @date 2018/12/25
     * @time 18:28
     * @title 兑换数据
     */
    public function exchangeCount()
    {
        $keyWords = input('get.keyWords');
        $startTime = input('get.start_time');
        $endTime = input('get.end_time');
        $type = input('get.type');
        $map = [];
        if(!empty($keyWords) || $keyWords == '0'){
            $key = input('get.key');
            if($key == 'u_id'){
                $user_id = User::where('u_id',$keyWords)->value('id');
                if($user_id)
                    $map[] = ['user_id','=',$user_id];
            }
            if($key == 'nickname'){
                $user_id = User::where('nickname','like','%'.$keyWords.'%')->column('id');
                if($user_id)
                    $map[] = ['user_id','in',$user_id];
            }
        }
        if(!empty($startTime) && !empty($endTime)){
            $startTime = strtotime($startTime);
            $endTime = strtotime($endTime);
            if($startTime <= $endTime)
                $map[] = ['create_time','between',[$startTime,$endTime]];
            else
                $map[] = ['create_time','between',[$endTime,$startTime]];
        }
        else if(!empty($startTime)){
            $startTime = strtotime($startTime);
            $map[] = ['create_time','>=',$startTime];
        }
        else if(!empty($endTime)){
            $endTime = strtotime($endTime);
            $map[] = ['create_time','<=',$endTime];
        }

        if($type ==1){

            $moneyCount = $this->where($map)->sum('exchange_money');
            if(!$moneyCount) $moneyCount = 0;
            return ['code'=>100,'data'=>$moneyCount];
        }

        if($type==2){

            $diamondsCount = $this->where($map)->sum('exchange_diamonds');
            if(!$diamondsCount) $diamondsCount = 0;
            return ['code'=>200,'data'=>$diamondsCount];
        }

    }

    public function exchangeDetail()
    {
        $user_id = input('user_id');

        $page_config = [

            'page'=>input('page',1),
            'list_rows'=>input('limit',25),
            'query'=>[
                'user_id' => $user_id,
            ]
        ];

        $data = $this->where('user_id','eq',$user_id)->order('created_at DESC')->paginate($page_config);

        $userInfo = User::where('user_id','eq',$user_id)->field('u_number as u_id,nickname')->find();
        foreach($data as $k => $v){

            $data[$k]['u_id'] = $userInfo['u_id'];
            $data[$k]['nickname'] = preg_replace_callback('/@E(.{6}==)/', function ($r) {
                return base64_decode($r[1]);
            }, $userInfo['nickname']);

        }
        return $data;
    }
}